//
//  XZFoodController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZFoodController.h"
#import "DwtTextFiled.h"
#import "DwtFoodSelectController.h"
#import "XZNavigationController.h"
#import "XZBaseTool.h"
#import "DwtFootCategoryParam.h"
#import "DwtFootCategoryParent.h"
#import "DwtFootCategoryList.h"
#import "DwtFootCategoryReturn.h"
#import "XZFunctionKey.h"
#import "MJRefresh.h"
#import "DwtHeaderView.h"
#import "DwtTableViewCell.h"
#import "DwtFoodListController.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"
#import "MBProgressHUD+MJ.h"

@interface XZFoodController ()<UITableViewDataSource,UITableViewDelegate,DwtHeaderViewDelegate>

@property (nonatomic,strong)NSArray *footCategoryParentes;
@property (nonatomic,weak)DwtTextFiled *selectTextFile;
@end

@implementation XZFoodController

-(NSArray *)footCategoryParentes{
    if (_footCategoryParentes==nil) {
        _footCategoryParentes = [NSArray array];
    }
    return _footCategoryParentes;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    /**设置导航栏按钮*/
    [self setupNavBar];

    
    DwtFootCategoryParam *footCategoryParam = [[DwtFootCategoryParam alloc]init];
    footCategoryParam.key = [XZFunctionKey foodKey];
    //    footCategoryParam.parentid=@"";
    //    footCategoryParam.dtype=@"json";
    [XZBaseTool getWithUrl:@"http://apis.juhe.cn/cook/category" parameter:footCategoryParam resultClass:[DwtFootCategoryReturn class] success:^(DwtFootCategoryReturn *footCategoryReturn) {
        _footCategoryParentes = footCategoryReturn.result;
        [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD showError:@"请检查网络"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        return;

        
        _footCategoryParentes = nil;
        [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
    }];

    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
}


#pragma 设置导航栏
/*设置导航栏按钮*/
- (void)setupNavBar{
    // 设置搜索框
    DwtTextFiled *selectTextFile=[[DwtTextFiled alloc]init];
    selectTextFile.width=220;
    selectTextFile.height=30;
    self.navigationItem.titleView=selectTextFile;
    self.selectTextFile=selectTextFile;
    
    // 设置导航栏按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStyleBordered target:self action:@selector(select)];
}
/**返回*/
- (void)cancel
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**搜索*/
-(void)select{
    
    if ([self.selectTextFile.text isEqual:@""]) {
        [MBProgressHUD showError:@"请输入需要搜索的文字"];
        [self.tableView headerEndRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
        return ;
        
    }
    DwtFoodSelectController *foodSelectController = [[DwtFoodSelectController alloc] init];
    foodSelectController.selectText=self.selectTextFile.text;
    XZNavigationController *nav = [[XZNavigationController alloc]initWithRootViewController:foodSelectController];
    [self presentViewController:nav animated:YES completion:nil];

}

#pragma mark - Table view data source
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.footCategoryParentes.count;
    
}

// 设置每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 1.取出对应的组模型
    DwtFootCategoryParent *footCategoryParent=self.footCategoryParentes[section];
    // 2.返回对应组中的好友数
    //    return qqGroup.friends.count;
    //    return 0;
    if (footCategoryParent.isOpen) {
        // 代表要展开
        return footCategoryParent.list.count;
    }else
    {
        // 代表要合拢
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 获取数据
    DwtFootCategoryParent *footCategoryParent =self.footCategoryParentes[indexPath.section];
    DwtFootCategoryList *footCategoryList = footCategoryParent.list[indexPath.row];
    
    DwtTableViewCell *cell = [DwtTableViewCell cellWithTableView:tableView];
    cell.footCategoryList=footCategoryList;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

// 当一个分组标题进入视野的时候就会调用该方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //    1.创建头部视图, 头部视图有默认的宽高,是系统自动设置
    DwtHeaderView *headerView = [DwtHeaderView headerViewWithTableView:tableView];
    //    2.传递模型
    DwtFootCategoryParent *footCategoryParent=self.footCategoryParentes[section];
    headerView.footCategoryParent = footCategoryParent;
    
    // 设置当前控制器为代理
    headerView.delegate = self;
    
    //    3.返回头部视图
    return headerView;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.selectTextFile endEditing:YES];
    
    // 获取数据
    DwtFootCategoryParent *footCategoryParent =self.footCategoryParentes[indexPath.section];
    DwtFootCategoryList *footCategoryList = footCategoryParent.list[indexPath.row];

    
    
    DwtFoodListController *foodListController = [[DwtFoodListController alloc]init];
    foodListController.id=footCategoryList.id;
    [self.navigationController pushViewController:foodListController animated:YES];
}



#pragma mark - NJHeaderViewDelegate
- (void)headerViewDidClickHeaderView:(DwtHeaderView *)headerView
{
    [self.selectTextFile endEditing:YES];

    NSLog(@"按钮被点击了,DwtHeaderView=%d",headerView.footCategoryParent.isOpen);
    // 重新调用数据源的方法加载数据
    [self.tableView reloadData];
   
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.selectTextFile endEditing:YES];
}

@end
